package com.cskaoyan.crud;

import com.cskaoyan.utils.JDBCUtils;
import com.cskaoyan.utils.JDBCUtils2;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 创建日期: 2022/07/26 10:18
 *
 * @author ciggar
 *
 *
 * 使用JDBC进行查询
 */
public class QueryDemo {

    public static void main(String[] args) throws SQLException {

        // 获取连接
        Connection connection = JDBCUtils2.getConnection();

        // 获取statement
        Statement statement = connection.createStatement();


        // 发送SQL语句
        ResultSet resultSet = statement.executeQuery("select id as cid, name as cname,p_id as pid from city");

        // 解析结果集
        // 当游标成功向下移动的时候，返回true，当移动到最后一行之后，游标不能继续向下移动的时候，返回false
        while (resultSet.next()) {

            int cid = resultSet.getInt("cid");
            String cname = resultSet.getString("cname");
            int pid = resultSet.getInt("pid");


            System.out.println("cid:" + cid + ", cname:" + cname + ",pid:" + pid);

        }
        // 关闭资源
        JDBCUtils2.closeResource(statement,connection,resultSet);

    }


}
